package mmoon.mmoonstory.items;

import mmoon.mmoonstory.MMoonAll;
import mmoon.mmoonstory.EasyCommand;
import mmoon.mmoonstory.MMoonConfig;
import mmoon.mmoonstory.interfaces.InitItem;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.Objects;

public class ItemIronApple extends ItemFood implements InitItem {
    public static final String ObjID = "iron_apple";

    public ItemIronApple() {
        super(20, 20, true);
        this.setAlwaysEdible();
        this.setPotionEffect(new PotionEffect(MMoonAll.IRON_SKIN, 1200, 1), 1);
        this.setMaxDamage(9);
        this.init(ObjID);
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack) {
        return 16;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        playerIn.setActiveHand(handIn);
        return new ActionResult<>(EnumActionResult.SUCCESS, playerIn.getHeldItem(handIn));
    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving) {
        if (entityLiving instanceof EntityPlayer) {
            EntityPlayer entityplayer = (EntityPlayer) entityLiving;
            entityplayer.getFoodStats().addStats(this, stack);
            worldIn.playSound(null, entityplayer.posX, entityplayer.posY, entityplayer.posZ,
                    SoundEvents.ENTITY_PLAYER_BURP, SoundCategory.PLAYERS, 0.5F,
                    worldIn.rand.nextFloat() * 0.1F + 0.9F);
            this.onFoodEaten(stack, worldIn, entityplayer);
            entityplayer.addStat(Objects.requireNonNull(StatList.getObjectUseStats(this)));
            if (entityplayer instanceof EntityPlayerMP) {
                CriteriaTriggers.CONSUME_ITEM.trigger((EntityPlayerMP) entityplayer, stack);
            }
        }
        return stack;
    }

    @Override
    protected void onFoodEaten(ItemStack stack, World world, EntityPlayer player) {
        super.onFoodEaten(stack, world, player);
        if (!world.isRemote) {
            stack.damageItem(1, player);
            if (player.getAbsorptionAmount() > 0) {
                EasyCommand.lotsOfDamage(player, "starve", MMoonConfig.IRON_SKIN_DAMAGE);
            } else if (MMoonConfig.IRON_SKIN_RESISTANCE > 0) {
                player.setAbsorptionAmount(MMoonConfig.IRON_SKIN_RESISTANCE);
            }
        }
    }

    @Override
    @SideOnly(Side.CLIENT)
    public boolean hasEffect(ItemStack stack) {
        return true;
    }
}
